package top.bingoz.wanandroid.data.http.observer

import org.reactivestreams.Subscription
import top.bingoz.wanandroid.contract.Contract

/**
 * @author su
 * @date 2018/7/30
 * @description
 */
abstract class SimpleSubscriber<T>() : EmptySubscriber<T>() {

    private var mView: Contract.View? = null
    private var mLoading = false

    constructor(view: Contract.View?, loading: Boolean = false) : this() {
        mView = view
        mLoading = loading
    }

    override fun onSubscribe(s: Subscription) {
        super.onSubscribe(s)
        s.request(Long.MAX_VALUE)
        if (mLoading) {
            mView?.showloading()
        }

    }

    override fun onComplete() {
        super.onComplete()
        if (mLoading) {
            mView?.dismissloading()
        }
    }


    override fun onNext(data: T) {
        super.onNext(data)
    }

    override fun onError(e: Throwable) {
        super.onError(e)
        if (mLoading) {
            mView?.dismissloading()
        }
        mView?.showError(e.message)
    }
}